package com.cuteboy.admin.controller;

import com.cuteboy.admin.service.SysRolePermissionService;
import com.cuteboy.admin.service.SysRoleService;
import com.cuteboy.entity.SysRole;
import com.cuteboy.util.Constants;
import com.cuteboy.vo.Page;
import com.cuteboy.vo.ResponseVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/admin/role")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    /**
     * 跳转到单页面
     * @return
     */
    @RequiresPermissions(value = {"admin:role"})
    @RequestMapping("/torolemanage")
    public String torolemanage() {
        return "page/role";
    }

    @RequiresPermissions(value = {"admin:role:list"})
    @ResponseBody
    @RequestMapping("list")
    public ResponseVO page(@RequestParam(required = false) String name, Page page){

        List<SysRole> pages = sysRoleService.page(name, page.getPage(), page.getLimit());

        Integer totalNum = sysRoleService.countAll();
        return new ResponseVO(Constants.ResponseVo.Code.SUCCEESS, Constants.ResponseVo.msg.SUCCEESS, totalNum, pages);
    }

//    @RequiresPermissions(value = {"admin:role:info"})
    @RequiresPermissions(value = {"admin:role:list"})
    @ResponseBody
    @RequestMapping("info")
    public ResponseVO info(@RequestParam Integer id){
        SysRole sysRole = sysRoleService.getEntity(id);
        return new ResponseVO(Constants.ResponseVo.Code.SUCCEESS, Constants.ResponseVo.msg.SUCCEESS, sysRole);
    }

    /**
     * 新增
     * @param entity
     * @return
     */
    @RequiresPermissions(value = {"admin:role:save"})
    @ResponseBody
    @RequestMapping("save")
    public ResponseVO save(SysRole entity){
        SysRole sysRole = sysRoleService.save(entity);
        return new ResponseVO(Constants.ResponseVo.Code.SUCCEESS, Constants.ResponseVo.msg.SUCCEESS, sysRole);
    }

    /**
     * 编辑
     * @param entity
     * @return
     */
    @RequiresPermissions(value = {"admin:role:update"})
    @ResponseBody
    @RequestMapping("update")
    public ResponseVO update(SysRole entity){
        SysRole sysRole = sysRoleService.update(entity);
        return new ResponseVO(Constants.ResponseVo.Code.SUCCEESS, Constants.ResponseVo.msg.SUCCEESS, sysRole);
    }

    @RequiresPermissions(value = {"admin:role:delete"})
    @ResponseBody
    @RequestMapping("delete")
    public ResponseVO delete(@RequestParam Integer id){
        sysRoleService.delete(id);
        return new ResponseVO(Constants.ResponseVo.Code.SUCCEESS, Constants.ResponseVo.msg.SUCCEESS);
    }
}
